//package project.controller;
//
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.IOException;
//import java.io.InputStream;
//
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.apache.tomcat.util.http.fileupload.FileItem;
//import org.apache.tomcat.util.http.fileupload.RequestContext;
//import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
//import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
//
//
//public class UploadHandleServlet extends HttpServlet {
//
//	/**
//	 * 
//	 */
//	private static final long serialVersionUID = 4712282440476004478L;
//
//	@Override
//	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
//			throws ServletException, IOException {
//		String savePath = this.getServletContext().getRealPath("/WEB-INF/uploads");
//		File file = new File(savePath);
//		
//		if (file.exists() && !file.isDirectory()) {
//			System.out.println(savePath+" content doesn't exist, please create a new one!");
//			file.mkdir();
//		}
//		
//		String message = "";
//		try {
//			//1.Create a new DiskFIleItemFactory
//			DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
//			//2.Create a file upload resolver for this factory
//			ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
//			//3.Set the content encode type so it won't be messed up.
//			servletFileUpload.setHeaderEncoding("UTF-8");
//			//4.Judge whether the data submitted are the same as the data uploaded from the form.
//			if(!ServletFileUpload.isMultipartContent(req))
//				return;
//			
//			java.util.List<FileItem> list = servletFileUpload.parseRequest((RequestContext) req);
//			for(FileItem item : list){
//				//if the file sealed in fileItem is a common input item.
//				if(item.isFormField()){
//					String name = item.getFieldName();
//					String value = item.getString("UTF-8");
//					System.out.println(name + "equal" + value);
//				}else {
//					//if the file sealed in fileItem is an uploaded file.
//					String filename = item.getName();
//					System.out.println(filename);
//					if(filename == null || filename.trim().equals(""))
//						continue;
//					
//					filename = filename.substring(filename.lastIndexOf("\\")+1);
//					
//					InputStream inputStream = item.getInputStream();
//					FileOutputStream outputStream = new FileOutputStream(savePath + "\\" + filename);
//					//buffer stream
//					byte buffer[] = new byte[1024];
//					int length = 0;
//					while((length = inputStream.read(buffer)) > 0){
//						outputStream.write(buffer,0,length);
//					}
//					
//					inputStream.close();
//					outputStream.close();
//					//delete the temporary file
//					item.delete();
//					message = "Successfully upload the file!";
//				}
//			}
//			
//		} catch (Exception e) {
//			message = "Fail to upload the file!";
//			e.printStackTrace();
//		}
//		req.setAttribute("message", message);
//		req.getRequestDispatcher("/message.jsp").forward(req, resp);
//	}
//
//	@Override
//	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
//			throws ServletException, IOException {
//		doGet(req, resp);
//	}
//	
//}
